package com.addressbook.tests;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.addressbook.fw.ContactData;
import com.addressbook.fw.DateData;
import com.thoughtworks.xstream.XStream;

public class ContactDataGenerator {

	public static void main(String[] args) throws IOException {
		if (args.length < 3) {
			System.out.println("Please specify parameters: <amount of test data> <file> <format> ");
			return;
		}
		int amount = Integer.parseInt(args[0]);
		File file = new File(args[1]);
		String format = args[2];

		if (file.exists()) {
			System.out.println("File " + file + " is already exists, please, remove it manually");
			return;
		}

		List<ContactData> contacts = generateRandomContacts(amount);
		if ("csv".equals(format)) {
			saveContactsToCsvFile(contacts, file);
		} else if ("xml".equals(format)) {
			saveContactsToXmlFile(contacts, file);
		} else {
			System.out.println("Unknown format");
		}
	}

	private static void saveContactsToXmlFile(List<ContactData> contacts,
			File file) throws IOException {

		XStream xstream = new XStream();
		xstream.alias("contact", ContactData.class);
		String xml = xstream.toXML(contacts);
		FileWriter writer = new FileWriter(file);
		writer.write(xml);
		writer.close();
		System.out.println("Generation of File " + file.getName()
				+ " is completed");
	}

	@SuppressWarnings("unchecked")
	public static List<ContactData> loadsContactsFromXmlFile(File file)
			throws IOException {
		XStream xstream = new XStream();
		xstream.alias("contact", ContactData.class);
		return (List<ContactData>) xstream.fromXML(file);
	}

	private static void saveContactsToCsvFile(List<ContactData> contacts,
			File file) throws IOException {
		FileWriter writer = new FileWriter(file);
		for (ContactData contact : contacts) {
			writer.write(contact.getName() + ","
					+ contact.getLastName()	+ ","
					+ contact.getAddress() + ","
					+ contact.getHome() + ","
					+ contact.getMobile() + ","
					+ contact.getWork()  + ","					
					+ contact.getEmail() + ","
					+ contact.getEmail2() + "," 
					+ contact.getBirthDay() + "," 
					+ contact.getBirthMonth() + "," 
					+ contact.getBirthYear() + ","
					+ contact.getGroup() + "," 
					+ contact.getAddress2() + "," 
					+ contact.getHome2() + ",!\n");
		}
		writer.close();
		System.out.println("Generation of File " + file.getName()
				+ " is completed");
	}

	public static List<ContactData> loadsContactsFromCsvFile(File file)
			throws IOException {
		List<ContactData> list = new ArrayList<ContactData>();
		FileReader reader = new FileReader(file);
		BufferedReader bufferedReader = new BufferedReader(reader);
		String line = bufferedReader.readLine();
		while (line != null) {
			String[] part = line.split(",");
			ContactData contact = new ContactData()
					.withName(part[0])
					.withLastName(part[1])
					.withAddress(part[2])
					.withHome(part[3])
					.withMobile(part[4])
					.withWork(part[5])
					.withEmail(part[6])
					.withEmail2(part[7])
					.withBirthDay(part[8])
					.withBirthMonth(part[9])
					.withBirthYear(part[10])
					.withGroup(part[11])
					.withAddress2(part[12])
					.withHome2(part[13]);
			list.add(contact);
			line = bufferedReader.readLine();
		}

		bufferedReader.close();
		return list;
	}

	public static List<ContactData> generateRandomContacts(int amount) {
		List<ContactData> contacts = new ArrayList<ContactData>();

		DateData date = new DateData();
		for (int i = 0; i < 2; i++) {
			date.setRandomDate();
			ContactData contact = new ContactData()
					.withName(generateRandomString("name"))
					.withLastName(generateRandomString("surname"))
					.withAddress(generateRandomString("address"))
					.withHome(generateRandomString(""))
					.withMobile(generateRandomString(""))
					.withWork(generateRandomString(""))
					.withEmail(generateRandomString("asd@asd"))
					.withEmail2(generateRandomString("qwe@qwe"))
					.withBirthDay(date.getDay())
					.withBirthMonth(date.getMonth())
					.withBirthYear(date.getYear())
					// .withGroup("group_name");
					.withAddress2(generateRandomString("address2_"))
					.withHome2(generateRandomString(""));
			contacts.add(contact);
		}
		return contacts;
	}

	public static String generateRandomString(String basis) {
		Random rnd = new Random();
		if (rnd.nextInt(5) == 0) {
			return "";
		} else {
			return basis + rnd.nextInt();
		}
	}
}